grid_w = 40
grid_h = 40


def new_obj(position_object,p):
    r = random(0,3)
    ks = list(position_object.keys())
    objname = ks[int(r)]
    #p = get_grid_idx(random(0,w),random(0,h))
    position_object[objname].append(p)

def try_hit_brick(p,white_bricks,position_object):
    if p in white_bricks:
        white_bricks.remove(p) 
        print('hit brick')
        new_obj(position_object,p)
        return True
    return False

def make_map(position_brick):
    for row in range(4, 11, 2):
        for col in range(0, 15, 2):
            position_brick['gongfu'].append((col, row))
    # for x,y in  [(5,5),(7,5),(9,5),(11,5)]:
    #     position_brick['gongfu'].append((x, y))
    # return  
    
    # for row in range(0, 13):
    #     for col in range(0, 15):
    #         x,y = (7,6)
    #         if abs(x - col) + abs(y-row) == 6:
    #             position_brick['gongfu'].append((col, row))
def new_player_state():
    return {'obj': {'number': 0, 'speed': 0, 'power': 0}, 'speed': 10,'s':0,'current_direction':'down','isBot':True}


def get_grid_centre(row,col):
    return row*grid_h+ 0.5*grid_h, col * grid_w + 0.5 * grid_w

def get_grid_topleft(row,col):
    return row*grid_h   , col * grid_w  

def get_grid_idx(x,y):
    return  int(x/grid_w),int(y/grid_h)


def get_bot_bubble_limit():
    return 100


def bot_put_bubble(bot_dict, p,bubble_time,bubble_power):
 
    if len(bot_dict['bubbles']) < get_bot_bubble_limit():
        if p not in bubble_time:
            #bubble_log[p] = True
            bot_dict['bubbles'].append(p)
            bubble_time[p] = millis()
            bubble_power[p] = bot_dict['player_state']['obj']['power'] + 2
            print('BP')
            print(bubble_power)


def bot_put_bubble_at_current_position(bot,bubble_time,bubble_power):
    botx_grid, boty_grid = get_grid_idx(bot['position'].x, bot['position'].y)
    p = (botx_grid, boty_grid)
    bot_put_bubble(bot, p,bubble_time,bubble_power)
    
def get_inverted_obj_map(position_object):
    d = {}
    for k ,ps in position_object.items():
        for p in ps:
            d[p] = k
    return d

def is_player_on_obj(pV,obj_map):
    player_p= get_grid_idx(pV.x,pV.y)
    return  obj_map.get(player_p),player_p
        
def bot_add_obj(bot, obj):
    bot['player_state']['obj'][obj]+=1
    return bot
